﻿var dragObjectHTML = null;
var dragObjectPosition = null;
var dragMouseCoords = null;
var dragDocumentOnMouseMove = null;
var dragDocumentOnMouseUp = null;

var Drag =
{
    start: function(objectHTML, ev) 
    {
        ev = ev || window.event;
            
        dragObjectHTML = objectHTML;
        dragObjectPosition = Common.getPosition(objectHTML);
        dragMouseCoords = Common.getMouseCoords(ev);
        
        dragDocumentOnMouseMove = document.onmousemove;
        dragDocumentOnMouseUp = document.onmouseup;
        
        Common.addEventListener(document, "mousemove", Drag.drag, true);        
        Common.addEventListener(document, "mouseup", Drag.end, false);        
    },
    
    end: function()
    {
        dragObjectHTML = null;
        dragMouseCoords = null;
        dragObjectPosition = null;
        
        document.onmousemove = dragDocumentOnMouseMove;
        document.onmouseup = dragDocumentOnMouseUp;
        dragDocumentOnMouseMove = "";
        dragDocumentOnMouseUp = "";
    },

    drag: function(ev)
    {
        ev = ev || window.event;
        
        if (dragObjectHTML != null)
        {
            var coords = Common.getMouseCoords(ev);
            var top = coords.y - dragMouseCoords.y;
            var left = coords.x - dragMouseCoords.x;
            
            Common.setPosition(dragObjectHTML, dragObjectPosition.x + left, dragObjectPosition.y + top)
            Common.emptySelection();
            
            return false;
        }
    }
}
